package org.cocos2dx.javascript;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.vivo.mobilead.interstitial.InterstitialAdParams;
import com.vivo.mobilead.interstitial.VivoInterstitialAd;
import com.vivo.mobilead.listener.IAdListener;
import com.vivo.mobilead.model.BackUrlInfo;
import com.vivo.mobilead.model.VivoAdError;
import com.vivo.mobilead.util.VOpenLog;

import java.util.Timer;
import java.util.TimerTask;


/**
 * @author xwc
 * @date 2017/11/14
 */

public class InterstitialActivity {
    private static final String TAG = "InterstitialActivity";
    public VivoInterstitialAd mVivoInterstitialAd;

    private static InterstitialActivity instance;
    private InterstitialActivity () {}
    public static InterstitialActivity getInstance() {
        if (instance == null) {
            instance = new InterstitialActivity();
        }
        return instance;
    }
    public AppActivity app = null;
    private Context context = null;
    public boolean show=false;
    private boolean AdReady=false;
    public boolean bannerShow=false;
    public void initApp (AppActivity _app, Context _context) {
        context=_context;
        app = _app;
        fetchInterstitialAd();
    }

    public void fetchInterstitialAd() {

        Timer _timer=new Timer();
        TimerTask _task=new TimerTask() {
            @Override
            public void run() {
                if(AdReady==true){
                    return;
                }
                Log.d(Config.TAG, "加载插屏广告");
                InterstitialAdParams.Builder builder = new InterstitialAdParams.Builder(Config.NativeExpressId);
                String backUrl = "vivobrowser://browser.vivo.com";
                String btnName = "test";
                BackUrlInfo backUrlInfo = new BackUrlInfo(backUrl, btnName);
                builder.setBackUrlInfo(backUrlInfo);
                mVivoInterstitialAd = new VivoInterstitialAd(app, builder.build(), mIAdListener);
                mVivoInterstitialAd.load();
            }
        };
        _timer.scheduleAtFixedRate(_task,0,30000);
    }
    public void showAd(){

        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mVivoInterstitialAd.showAd();
                Log.d(Config.TAG, "展示插屏广告");
            }
        });
    }

    private IAdListener mIAdListener = new IAdListener() {
        @Override
        public void onAdShow() {
            Log.d(Config.TAG, "插屏广告展示成功");
            show=true;
            if(WDNativePlatform.getInstance().mBannerContainer.getVisibility()==View.VISIBLE){
                bannerShow=true;
                WDNativePlatform.getInstance().mBannerContainer.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void onAdFailed(VivoAdError error) {
            show=false;
            AdReady=false;
            if(bannerShow==true){
                bannerShow=false;
                WDNativePlatform.getInstance().mBannerContainer.setVisibility(View.VISIBLE);
            }
            Log.d(Config.TAG, "插屏广告加载失败：" + error);
        }

        @Override
        public void onAdReady() {
            Log.d(Config.TAG, "插屏广告加载成功");
            AdReady=true;
        }

        @Override
        public void onAdClick() {
        }

        @Override
        public void onAdClosed() {
            show=false;
            AdReady=false;
            if(bannerShow==true){
                bannerShow=false;
                WDNativePlatform.getInstance().mBannerContainer.setVisibility(View.VISIBLE);
            }
            Log.d(Config.TAG, "插屏广告关闭");
        }
    };

}
